package com.yishun.springboot.dao;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/*
实名制对接测试
 */

@Slf4j
public class CWRServiceTest {
	private String api_secret = "xxx";
	private String api_key = "xxx";
	private String client_serial = "xxx";
	private String timestamp = "2019-07-28 15:31:14";
	private String api_version = "1.0";
	private String Project_ID = "xxx";
	private String SUID = "xxx";
	private String companyName = "xxx";

	/**
	 * 签名的生成
	 *
	 * @throws NoSuchAlgorithmException
	 */
	@Test
	public void createSignature() throws NoSuchAlgorithmException {
		String signature = getSignature(api_secret,
				"{\"entry_time\":\"2019-07-01 02:56:05\",\"Legal_Person\":\"罗世威\",\"Type\":\"009\",\"Project_ID\":\"XM2013031101\",\"Company_Name\":\"中国建筑一局(集团)有限公司\"}",
				api_key, api_version, client_serial, timestamp);
		log.info(signature);
		// 如果body为空
		String signature2 = getSignature(api_secret, null, api_key, api_version, client_serial, timestamp);
		log.info(signature2);
	}

	@Test
	public void f2() {
		String url = "http://127.0.0.1:80/CWRService/AddCompany";
		JSONObject json = new JSONObject();
		json.put("Project_ID", "XM2013031101");
		json.put("Company_Name", "中国建筑一局(集团)有限公司");
		json.put("SUID", "91130403308243631c");
		json.put("Legal_Person", "罗世威");
		json.put("Type", "009");// 单位类型
		json.put("entry_time", "2019-07-02 09:53:33");
		String body = json.toString();
		try {
			String signature = getSignature("6ea276ae113c4384881570b1ac4d6ac1", body,
					"b369680e95b6495a8bdd5544f3f8c5b7", "1.0", "b369680e95b6495a8bdd5544f3f8c5b7",
					"2019-07-02 09:53:33");
			Map<String, Object> param = new HashMap<>();
			param.put("api_version", "1.0");
//            param.put("api_secret", "6ea276ae113c4384881570b1ac4d6ac1");
			param.put("client_serial", "b369680e95b6495a8bdd5544f3f8c5b7");
			param.put("api_key", "b369680e95b6495a8bdd5544f3f8c5b7");
			param.put("timestamp", "2019-07-02 09:53:33");
			param.put("signature", signature);
			param.put("body", body);
			log.info("pararm:" + param.toString());
			String info = HttpUtil.post(url, param);
			log.info(info);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void addCompany() {
		String url = "http://127.0.0.1:80/CWRService/AddCompany";
		JSONObject json = new JSONObject();
		json.put("Project_ID", Project_ID);
		json.put("Company_Name", companyName);
		json.put("SUID", SUID);
		json.put("Legal_Person", "香克斯");
		json.put("Type", "016");// 单位类型
		json.put("entry_time", timestamp);
		String body = json.toString();
		try {
			String signature = getSignature(api_secret, body, api_key, api_version, client_serial, timestamp);
			Map<String, Object> param = new HashMap<>();
			param.put("api_version", api_version);
			param.put("client_serial", client_serial);
			param.put("api_key", api_key);
			param.put("timestamp", timestamp.toString());
			param.put("signature", signature.toString());
			param.put("body", body);
			String info = HttpUtil.post(url, param);
			log.info(info);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * AddTeam接口
	 */
	@Test
	public void addTeam() {
		String url = "http://127.0.0.1:80/CWRService/AddTeam";
		JSONObject json = new JSONObject();
		json.put("Project_ID", Project_ID);
		json.put("team_name", "焊工班007");
		json.put("team_type_order", "waterproof");// 3.1.4接口
		json.put("team_type_name", "防水班");// 3.1.4接口
		json.put("emp_company", companyName);
		json.put("SUID", SUID);
		String body = json.toString();
		try {
			String signature = getSignature(api_secret, body, api_key, api_version, client_serial, timestamp);
			Map<String, Object> param = new HashMap<>();
			param.put("api_version", api_version);
			param.put("client_serial", client_serial);
			param.put("api_key", api_key);
			param.put("timestamp", timestamp);
			param.put("signature", signature);
			param.put("body", body);
			log.info("here");
			log.info("parm:" + param.toString());
			log.info("here2");
			String info = HttpUtil.post(url, param);
			log.info(info);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void RegisterEmployee() {
		String url = "http://127.0.0.1:80/CWRService/RegisterEmployee";
		JSONObject json = new JSONObject();
		json.put("Project_ID", Project_ID);
		json.put("id_code", "110101198001010053");
		json.put("id_photo", "007d9551f4154c30a2e84cc4c729110b");// 图片接口获取，上传图片返回的id
		json.put("emp_name", "陈陈陈");
		json.put("emp_phone", "15006017815");
		json.put("emp_nativeplace", "北京北京");
		json.put("emp_nation", "壮族");
		json.put("match_flag", "Y");
		json.put("facephoto", "0157de08946b420397fc78272676d6d3");
		json.put("emp_company", companyName);
		json.put("SUID", SUID);
		json.put("team_id", "e047325191ec4a1db1f970a3affa15ad");// team_id 来自 ADDTEAM接口的返回值
		json.put("team_name", "焊工班007");
		json.put("emp_category", "00");
		json.put("job_order", "f6bb2c573451494c9014d161231ba835");
		json.put("job_name", "电焊工");
		json.put("contract_status", "1");
		String body = json.toString();
		try {
			String signature = getSignature(api_secret, body, api_key, api_version, client_serial, timestamp);
			Map<String, Object> param = new HashMap<>();
			Map<String, Object> param2 = new HashMap<>();
			param.put("api_version", api_version);
			param.put("client_serial", client_serial);
			param.put("api_key", api_key);
			param.put("timestamp", timestamp);
			param.put("signature", signature);
			param.put("body", body);
			String info = HttpUtil.post(url, param2);
			log.info(info);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void UploadPassedLog() {
		String url = "http://127.0.0.1:80/CWRService/UploadPassedLog";
		JSONObject json = new JSONObject();
		json.put("Project_ID", Project_ID);
		json.put("Device_ID", "0001");

		List<JSONObject> passedlog_list = new ArrayList<>();
		JSONObject passedlog = new JSONObject();
		passedlog.put("data_id", api_key + UUID.randomUUID().toString().replace("-", ""));// 数据的唯一id
		passedlog.put("person_type", "1");
		passedlog.put("person_id", "110101198001010053");
		passedlog.put("person_name", "陈陈陈");
		passedlog.put("passed_time", timestamp);
		passedlog.put("direction", "in");
		passedlog.put("way", "1");
		passedlog.put("site_photo", "026547d8f4c54efcaab323cb98973d23");
		passedlog.put("address", "北京天安门南路西北路口东大道");
		passedlog_list.add(passedlog);

		json.put("passedlog_list", passedlog_list);
		String body = json.toString();
		try {
			String signature = getSignature(api_secret, body, api_key, api_version, client_serial, timestamp);
			Map<String, Object> param = new HashMap<>();
			param.put("api_version", api_version);
			param.put("client_serial", client_serial);
			param.put("api_key", api_key);
			param.put("timestamp", timestamp);
			param.put("signature", signature);
			param.put("body", body);
			String info = HttpUtil.post(url, param);
			log.info(info);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param api_secret    授权秘钥
	 * @param body          请求体
	 * @param api_Key       授权帐号
	 * @param api_Version   API协议版本
	 * @param client_Serial 第三方系统编号 可以取值
	 * @param timeStamp     时间戳，格式为yyyy-MM-dd hh:mm:ss
	 * @return boolean
	 * @Description:拼接加密参数
	 * @author fanghe 2017年3月25日 上午11:44:50
	 */
	private static String getSignature(String api_secret, String body, String api_Key, String api_Version,
			String client_Serial, String timeStamp) throws NoSuchAlgorithmException {
		StringBuilder str4MD5 = new StringBuilder();
		str4MD5.append(api_secret).append("api_key").append(api_Key).append("api_version").append(api_Version)
				.append("body").append(StrUtil.isEmpty(body) ? "{}" : body).append("client_serial")
				.append(client_Serial).append("timestamp").append(timeStamp).append(api_secret);
		String str = GetMD5Code(str4MD5.toString());
		return str;
	}

	/**
	 * Description: 获取32位大写MD5加密
	 *
	 * @param strObj 原文
	 * @return 32位大写MD5加密
	 * @see
	 */
	public static String GetMD5Code(String strObj) throws NoSuchAlgorithmException {
		String resultString = null;
		resultString = new String(strObj);
		MessageDigest md = MessageDigest.getInstance("MD5");
		Charset charset = Charset.forName("UTF-8");
		// md.digest() 该函数返回值为存放哈希值结果的byte数组
		resultString = byteToString(md.digest(strObj.getBytes(charset)));
		return resultString;
	}

	/**
	 * Description: 转换字节数组为16进制字串
	 *
	 * @param bytes 字节数组
	 * @return 16进制字串
	 * @see
	 */
	private static String byteToString(byte[] bytes) {
		StringBuffer sBuffer = new StringBuffer();
		for (int i = 0; i < bytes.length; i++) {
			sBuffer.append(byteToArrayString(bytes[i]));
		}
		return sBuffer.toString();
	}

	/**
	 * Description: 返回形式为数字跟字符串(大写)
	 *
	 * @param bByte 字节
	 * @return 返回形式为数字跟字符串(大写)
	 * @see
	 */
	private static String byteToArrayString(byte bByte) {
		int iRet = bByte;
		if (iRet < 0) {
			iRet += 256;
		}
		int iD1 = iRet / 16;
		int iD2 = iRet % 16;
		return strDigits[iD1] + strDigits[iD2];
	}

	/**
	 * 全局数组
	 */
	private final static String[] strDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D",
			"E", "F" };
}
